package com.example.demo.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * Copyright(C), 2020-2021, AmbroseCdMeng
 * FileName:    SysPermission
 * Author:      AmbroseCdMeng
 * Date:        2021/3/4 10:31
 * Description: 权限实体
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间        版本号           描述
 */
@Entity
@Data
public class SysPermission implements UserDetails {
    /**
     * 主键。自动增长
     */
    @Id
    @GeneratedValue
    private long id;

    /**
     * 名称
     */
    private String name;

    /**
     * 资源类型。[menu|button]
     */
    @Column(columnDefinition = "enum('menu', 'button')")
    private String resourceType;

    /**
     * 资源路径
     */
    private String url;

    /**
     * 权限字符串。
     *  menu 例子：
     *      role: *;
     *  button 例子：
     *      role: create
     *      role:update
     *      role:delete
     *      role:view
     */
    private String permission;

    /**
     * 父编号
     */
    private Long parentId;

    /**
     * 父编号列表
     */
    private String parentIds;

    /**
     * 是否启用。默认：是
     */
    private Boolean enabled = Boolean.TRUE;

    @Transient
    private List permissions;

    @ManyToMany
    @JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "permissionId")}, inverseJoinColumns = {@JoinColumn(name = "roleId")})
    private List<SysRole> roles;

    public List GetPermissions(){
        return Arrays.asList(this.permission.trim().split("|"));
    }

    public void setPermissions(List permissions){
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }
}
